package tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class GetAdjMatrix {
	/* Precondition:Set up the path of adjacency matrix file in Settings.java,in which file only contain integers.
	 * Postcondition:Return an adjacency matrix, whose rows and columns are equal to the number of vertexes. 
	 */
	public static int[][] getAdjMatrix(){
		int[][] vertexMatrix = GetVertex.getVertex();
		int rows = vertexMatrix.length;
		int[][] adjMatrix = new int[rows][rows];
		for(int i = 0; i < rows; i++)
			for(int j = 0; j < rows; j++){
				adjMatrix[i][j] = 0;
			}
		File adjMatrixFile = new File(Settings.nodesFilePath);
		try {
			FileReader fr = new FileReader(adjMatrixFile);
			BufferedReader br = new BufferedReader(fr);
			for(int i = 0; i < rows; i++){
				String line;
				try {
					line = br.readLine();
					String[] arr = line.split(" ");
					for(int j = 0; j < arr.length; j++){
						int temp = Integer.parseInt(arr[j]);
						adjMatrix[i][temp] = 1;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}			
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return adjMatrix;
	}
}
